const app = getApp();
var orderSn = '';
var weight = 0;
var price = 0;
var typeCode = 1;
var statusesJson ="";
var util = require('../../utils/util.js');  

Page({
  data: {
    navTab: ["待处理", "在路上", "已完成"],
    currentNavtab: "0",
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    feed: [],
    feed_length: 0,
    orderList: [],
    userType:'0',
    hiddedetailIput:true,
    weight: null,
    price:null,
    typeIndex:0,
    typeItems: [
      { type: "1", name: "黄纸" },
      { type: "2", name: "花纸" },
      { type: "3", name: "报纸书刊" },
      { type: "4", name: "黄纸、花纸混合" },
      { type: "5", name: "其他" }
    ],
    typeName:"黄纸"
  },
  onLoad: function () {
    console.log('onLoad')
  },
  switchTab: function (e) {
    var index = e.currentTarget.dataset.idx;
    console.log(index)
    if (index == 0){
      statusesJson = '[0,1]'
    }else if(index == 1){
      statusesJson = '[2]'
    }else{
      statusesJson = '[3,4]'
    }
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
    var reqUrl = "";
    if (app.globalData.userType == "1") {
      reqUrl = "/collect-scrap/queryWagonerOrder";
    } else {
      reqUrl = "/collect-scrap/queryUserOrder";
    }
    var that = this;
    wx.request({/**通过code获取openid**/
      url:   getApp().globalData.server_support + reqUrl,
      data: statusesJson,
      header: {
        'token': getApp().globalData.token
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          if (index == 0) {
            that.setData({ orderList: res.data.data });
          } else if (index == 1) {
            that.setData({ orderList1: res.data.data });
          } else {
            that.setData({ orderList2: res.data.data });
          }
        } else {
          console.log("未填写过地址");
        }
      }
    })
  },
  selOrderInfo:function(e){
    console.log("查看订单信息");
    console.log(e.currentTarget.dataset.ordersn);
    var ordreSn = e.currentTarget.dataset.ordersn;
    wx.navigateTo({
      url: '../../pages/orderinfo/orderinfo?orderSn=' + ordreSn ,
    })
  }, 
  navigateBackFunc: function () {
  },
  onShow:function(){
    console.log('开始查询订单信息')
    console.log(getApp().globalData.token)
    var statusesJson = '[0,1]';
    var reqUrl = "";
    if (app.globalData.userType == "1"){
      reqUrl = "/collect-scrap/queryWagonerOrder";
    }else{
      reqUrl = "/collect-scrap/queryUserOrder";
    }
    var that = this;
    that.setData({ userType: app.globalData.userType });
    wx.request({/**通过code获取openid**/
      url:  getApp().globalData.server_support + reqUrl,
      data: statusesJson,
      header: {
        'token': getApp().globalData.token
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          that.setData({ orderList: res.data.data });
        } else {
          console.log("未填写过地址");
        }
      }
    })
  },
  doPhone:function(e){
    var phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone, //此号码并非真实电话号码，仅用于测试  
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })  
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    setTimeout(function () {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      var reqUrl = "";
      if (app.globalData.userType == "1") {
        reqUrl = "/collect-scrap/queryWagonerOrder";
      } else {
        reqUrl = "/collect-scrap/queryUserOrder";
      }
      var that = this;
      wx.request({/**通过code获取openid**/
        url: getApp().globalData.server_support + reqUrl,
        data: statusesJson,
        header: {
          'token': getApp().globalData.token
        },
        method: 'POST',
        success: function (res) {
          console.log(res.data);
          if (res.data.success) {
            that.setData({ orderList: res.data.data });
          } else {
            console.log("未填写过地址");
          }
        }
      })
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
  },
  receiveOrder:function(e){
    var that = this;
    console.log(e.currentTarget.dataset);
    wx.request({
      url:   getApp().globalData.server_support + "/collect-scrap/receiveOrder",
      data: e.currentTarget.dataset.ordersn,
      method: 'POST',
      header: {
        'token': getApp().globalData.token
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          console.log("接单成功")
          that.setData({
            currentNavtab:1
          });
          that.queryOrderList(1,'[2]')
        } else {
          console.log("接单失败")
        }

      },
      fail: function (res) {
       
      }
    })
  },
  queryOrderList: function (tabIndex,statusesJson){
    var reqUrl = "";
    if (app.globalData.userType == "1") {
      reqUrl = "/collect-scrap/queryWagonerOrder";
    } else {
      reqUrl = "/collect-scrap/queryUserOrder";
    }
    var that = this;
    that.setData({ userType: app.globalData.userType });
    wx.request({/**通过code获取openid**/
      url:   getApp().globalData.server_support + reqUrl,
      data: statusesJson,
      header: {
        'token': getApp().globalData.token
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          if (tabIndex == 2){
            that.setData({ orderList2: res.data.data });
          } else if(tabIndex == 1){
            that.setData({ orderList1: res.data.data });
          }
        } else {
          console.log("未填写过地址");
        }
      }
    })
  },
  writeDetail:function(e){
    var that = this;
    console.log("填写订单明细");
    this.setData({
      hiddedetailIput: !this.data.hiddedetailIput
    }) ,
    orderSn = e.currentTarget.dataset.ordersn;

  },
  //取消按钮  
  cancel: function () {
    this.setData({
      hiddedetailIput: true
    });
  },
  //确认  
  confirm: function () {
    var that = this;
    var reqJson = '[{"type":"' + typeCode+ '","orderSn":"' + orderSn + '","weight":' + weight + ',"price":' + price + '}]';
    console.log(reqJson);
    wx.request({
      url:  getApp().globalData.server_support + "/collect-scrap/saveUserOrderDetail",
      data: reqJson,
      method: 'POST',
      header: {
        'token': getApp().globalData.token
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          console.log("填写明细成功")
          that.setData({
            currentNavtab: 1
          });
        } else {
          console.log("填写明细失败")
        }

      },
      fail: function (res) {

      }
    })
    that.setData({
      hiddedetailIput: true
    })
  },
  weightInput:function(e){
    weight = e.detail.value
    console.log()
  },
  priceInput: function (e) {
    price = e.detail.value
  },
  bindPickerChange: function (e) {
    console.log("开始废纸类型。");
    console.log(e.detail.value);
    var data_index = parseInt(e.detail.value);
    console.log(this.data.typeItems[data_index].type);
    console.log(this.data.typeItems[data_index].name);
    typeCode = this.data.typeItems[data_index].type;
    this.setData({
      typeName: this.data.typeItems[data_index].name
    })
    console.log("结束废纸类型。");
  },
  finishOrder: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset);
    wx.request({
      url:   getApp().globalData.server_support + "/collect-scrap/finishOrder",
      data: e.currentTarget.dataset.ordersn,
      method: 'POST',
      header: {
        'token': getApp().globalData.token
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          console.log("接单成功")
          that.setData({
            currentNavtab: 2
          });
          that.queryOrderList(2, '[3,4]')
        } else {
          console.log("接单失败")
        }
      },
      fail: function (res) {

      }
    })
  },
  cancelOrder: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset);
    wx.request({
      url:  getApp().globalData.server_support + "/collect-scrap/cancelOrder",
      data: e.currentTarget.dataset.ordersn,
      method: 'POST',
      header: {
        'token': getApp().globalData.token
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          console.log("取消成功")
          that.setData({
            currentNavtab: 2
          });
          that.queryOrderList(2,'[3,4]')
        } else {
          console.log("取消失败")
        }

      },
      fail: function (res) {

      }
    })
  },
});
